<?php
	// Task 1: retry data
	$skillName = get_input('skillName');
	$skillDescription = get_input('description');
	/*
	$checkbox = get_input('group2checkbox');
	
	$radio = get_input('group2');
	system_message("".$checkbox.$radio);
	
	forward(REFERER);
	*/
	// check if skill name is valid
	if(!empty($skillName))
	{
		// Task 2: Check whether there is any skill have the same name with the skill that you want to create.
		$skillExisted = elgg_get_entities(array(
				'type' => 'object',
				'subtype' => 'skill',
		));
		
		if(!isExistTitle($skillExisted, $skillName)){
			// Task 3: save the skill to database
			$newSkill = new ElggObject();
			$newSkill->subtype = "skill";
			$newSkill->title = $skillName;
			$newSkill->description = $skillDescription;
			$newSkill->access_id = ACCESS_PUBLIC;
			$newSkill->save();
			system_message("Saved");
			forward(REFERER);
		} else {
			system_message("Existed");
			forward(REFERER);
		}
		
	} else {
		register_error(elgg_echo('skill:stringInvalid', array("Skill's name")));
		forward(REFERER);
	}
	
	function isExistTitle($skillsAraay, $title){
		$result  = count($skillsAraay);		
		for ($i = 0; $i < $result; $i++)
		{
			if($skillsAraay[$i]->title == $title){
				return true;
			}
		}
		
		return false;
	}
	
?>